package borrar;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestCNE {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, 
    IOException, XPathExpressionException{
		
		StringBuffer contenido = new StringBuffer();
		
		try {
	    	
	    	String nacionalidad = "V";
	    	String cedula = "14362641";
	    	
	        URL url = new URL("http://www.cne.gov.ve/web/registro_electoral/ce.php?nacionalidad=" + nacionalidad + "&cedula=" + cedula);
	        
	        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
	        String line;
	        int i = 0;
	        while ((line = reader.readLine()) != null) {
	        	if(i==33)
	        		contenido.append(line).append("\n");
	        	i++;
	        }
	        reader.close();

//	        System.out.println(contenido.toString());
	        
	    } catch (MalformedURLException e) {

	    } catch (IOException e) {

	    }
	    
	    
	    
	    DocumentBuilderFactory domFactory = 
	        DocumentBuilderFactory.newInstance();
	              domFactory.setNamespaceAware(true); 
	        DocumentBuilder builder = domFactory.newDocumentBuilder();

	        InputStream is = new ByteArrayInputStream(contenido.toString().getBytes("UTF-8"));	        
	        
	        Document doc = builder.parse(is);

	        XPath xpath = XPathFactory.newInstance().newXPath();

	        XPathExpression expr = xpath.compile("//td/strong/text()");

	        Object result = expr.evaluate(doc, XPathConstants.NODESET);
	        NodeList nodes = (NodeList) result;
	        StringBuffer respuesta = new StringBuffer();
	        for (int i = 0; i < nodes.getLength(); i++) {
	        	respuesta.append(nodes.item(i).getNodeValue());
	        	break;
	        }
	        String miout = respuesta.toString().trim();
	        System.out.println(";"+miout+";");

	}

}
